<?
if(!defined ('U_MOD'))
{
	echo"<meta http-equiv='refresh' content='0;url=/index.php'>";
	exit;
}
include 'topmenu.php';

$avialable_langs = explode(",", $option['avialable_langs']);
$avialable_langs_desc = explode(",", $option['avialable_langs_desc']);

$showForm=1;

if (!isset($FormError))
{
	$i=0;
	$FormError=array();
}

if (isset($act) && ($act==1 || $act==2))
{
	if ($act==1){$ActionUrl=1;  $ActionButton=_ADDNEW_;}
	if ($act==2){$ActionUrl=2;  $ActionButton=_EDITE_; $midActionUrl='&lid='.$lid;}

	if ($act==1 || $act==2)
	{
		if (in_array($lcode, $avialable_langs) && $act==1)
		{
			$FormError[$i]=_LANGUAGE_EXIST_;
			$i++;
		}elseif(!in_array($lid, $avialable_langs) && $act==2)
		{
			$FormError[$i]=_NO_LANG_FOR_EDITE_;
			$i++;
			$showForm=0;
			echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
		}else
		{
			if ($act==2 && (!isset($lid) || is_numeric($lcode)))
			{
				$FormError[$i]=_NO_LANG_FOR_EDITE_;
				$i++;
				$showForm=0;
				echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
			}

			if (!isset($lcode) || $lcode=='')
			{
				$FormError[$i]=_NO_LANG_CODE_;
				$i++;
			}else
				$lcode=trim($lcode);

			if (!isset($ldesc) || $ldesc=='')
			{
				$FormError[$i]=_NO_LANG_DESC_;
				$i++;
			}else
				$ldesc=trim($ldesc);


			if (empty($FormError))
			{
				if ($act==1)
				{
					$newLangArray=$option['avialable_langs'].",".$lcode;
					$newLangDescArray=$option['avialable_langs_desc'].",".$ldesc;

					$CopyDefaultLangTable1=CopyDefaultLangTable($lcode, $option['default_lang'], 1);
					$CopyDefaultLangTable2=CopyDefaultLangTable($lcode, $option['default_lang'], 2);

					$queryAddNewLang="UPDATE ".$PortalPref."options SET option_value = '".$newLangArray."' WHERE option_parametr ='avialable_langs'";
					$queryAddNewLangDesc="UPDATE ".$PortalPref."options SET option_value = '".$newLangDescArray."' WHERE option_parametr ='avialable_langs_desc'";

					$RedirectAdd='&lang='.$lcode.'';
				}elseif($act==2)
				{
					$RedirectAdd='';
					$i = array_search($lid, $avialable_langs);

					$newLangArray=$avialable_langs;
					$newLangDescArray=$avialable_langs_desc;
					$newLangArray[$i]=$lcode;
					$newLangDescArray[$i]=$ldesc;

					$newLangStr=implode(",", $newLangArray);
					$newLangDescStr=implode(",", $newLangDescArray);

					$queryAddNewLang="UPDATE ".$PortalPref."options SET option_value = '".$newLangStr."' WHERE option_parametr ='avialable_langs'";
					$queryAddNewLangDesc="UPDATE ".$PortalPref."options SET option_value = '".$newLangDescStr."' WHERE option_parametr ='avialable_langs_desc'";

					$queryRenameLangTable="RENAME TABLE ".$PortalPref."langs_".$lid." TO ".$PortalPref."langs_".$lcode."";

					if ($option['default_lang']==$avialable_langs[$i])
						$querySetNewDefault="UPDATE ".$PortalPref."options SET option_value = '".$lcode."' WHERE option_parametr ='default_lang'";
				}

				if (mysql_query($queryAddNewLang) && mysql_query($queryAddNewLangDesc))
				{
					if ($act==1)
					{
						if (!mysql_query($CopyDefaultLangTable1) || !mysql_query($CopyDefaultLangTable2))
						{
							mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs']."' WHERE option_parametr ='avialable_langs'");
							mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs_desc']."' WHERE option_parametr ='avialable_langs_desc'");
						}
					}

					if ($act==2)
					{
						if (!mysql_query($queryRenameLangTable))
						{
							mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs']."' WHERE option_parametr ='avialable_langs'");
							mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs_desc']."' WHERE option_parametr ='avialable_langs_desc'");
						}else
						{
							if(isset($querySetNewDefault) && !mysql_query($querySetNewDefault))
							{
								mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs']."' WHERE option_parametr ='avialable_langs'");
								mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs_desc']."' WHERE option_parametr ='avialable_langs_desc'");
								mysql_query("RENAME TABLE ".$PortalPref."langs_".$lcode." TO ".$PortalPref."langs_".$lid."");
							}
						}
					}
					$showForm=0;
					echo"<meta http-equiv='refresh' content='0;url=/admin.php?amk=".$amk."".$RedirectAdd."'>";
					//exit;
				}else
				{
					$FormError[$i]=_BAD_SQL_;
					$i++;
				}
			}
		}
	}
}elseif(isset($act) && $act==3)
{
	$ActionUrl=2;  $ActionButton=_EDITE_;

	if (!in_array($lid, $avialable_langs))
	{
		$FormError[$i]=_NO_LANG_FOR_EDITE_;
		$i++;
		$showForm=0;
		echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
	}else
	{
		$i = array_search($lid, $avialable_langs);
		$lcode=$avialable_langs[$i];
		$ldesc=$avialable_langs_desc[$i];
		$midActionUrl='&lid='.$lid;
	}
}else
{
	$ActionUrl=1;  $ActionButton=_ADDNEW_;
}

print '<p>';
getErrorList($FormError);
print '</p>';

if ($showForm==1)
{
	print '<form method="post" name="LangFrm" action="'.$portal_url.'/admin.php?amk='.$amk.'&apk='.$apk.'&act='.$ActionUrl.''.$midActionUrl.'">
		<table>
			<tr>
				<td>
					'._LANG_CODE_.'
				</td>
				<td>
					<input type="text" name="lcode" value="'.$lcode.'">
				</td>
			</tr>
			<tr>
				<td>'._LANG_DESC_.'</td>
				<td>
					<input type="text" name="ldesc" value="'.$ldesc.'">
					</td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="'.$ActionButton.'"></td>
			</tr>
		</table>
	</form>';
}


function CopyDefaultLangTable($langCode, $default_lang, $type)
{
	global $PortalPref, $BDCharSet;

	if ($type==1)
	{
		$sql = "\n"
	    . " CREATE TABLE `".$PortalPref."langs_".$langCode."` (`lang_id` int( 10 ) unsigned NOT NULL AUTO_INCREMENT ,\n"
    	. " `lang_module` varchar( 250 ) NOT NULL ,\n"
	    . " `lang_param` varchar( 250 ) NOT NULL ,\n"
    	. " `lang_value` text NOT NULL ,\n"
	    . " PRIMARY KEY ( `lang_id` ) ) ENGINE = MyISAM DEFAULT CHARSET = ".$BDCharSet.";\n"
    	. "\n";
	}
	if ($type==2)
    	$sql = "INSERT INTO `".$PortalPref."langs_".$langCode."` SELECT * FROM `".$PortalPref."langs_".$default_lang."`;";

	return $sql;
}
?>